//结算单
using('messager', function () {
    $.messager.progress({
        title: '提示',
        text: '系统加载中，请稍后....'
    });
    page_init();
});

var InvoiceTable;
var StatementsTable;
var LaborInputTable;
var OtherFeeEntryTable;

var LaborInputBox;
var OtherFeeEntryBox;
var StatementsBox;

var LaborInputTablerowindex=-1;
var OtherFeeEntryTableindex=-1;
var StatementsTablerowindex=-1;


var month_date=[
	{id:1,text:"1月"},
	{id:2,text:"2月"},
	{id:3,text:"3月"},
	{id:4,text:"4月"},
	{id:5,text:"5月"},
	{id:6,text:"6月"},
	{id:7,text:"7月"},
	{id:8,text:"8月"},
	{id:9,text:"9月"},
	{id:10,text:"10月"},
	{id:11,text:"11月"},
	{id:12,text:"12月"}
];

var proitems_data=[];

function page_init()
{
	using(["layout",'panel', 'dialog','datagrid','accordion','linkbutton','textbox','combobox',"datebox","form"], function () {
        system_init();
		$.messager.progress('close');
		$("#Statementslayout").layout({fit:true});
		Btn_init();
		Table_init();
		Box_init();
		
	});
}

function system_init()
{
	$.post(webpath+"OtherFeeEntry/getProitems",function(data){
		proitems_data=data;
		
		
		using("plugins/jquery.edatagrid.js", edatagrid_init);
		},"json");
}

//可编辑栏初始化
function edatagrid_init()
{
	LaborInputTable_init();
	OtherFeeEntryTable_init();
	StatementsTable_Init();
}

function Box_init()
{
	LaborInputBox=$("#LaborInputBox").dialog({
			title: '人工费录入',
			width: 700,
			height: 350,
			fit : true,
			closed: true,
			cache: false,
			modal: true,
			collapsible:true,
			buttons:[{
				text:'保存',
				iconCls:'icon-save',
				handler:LaborInputSaveBnt
			},{
				text:'关闭',
				iconCls:'icon-no',
				handler:function(){LaborInputBox.dialog("close");}
			}]
		});
	$("#LaborInputBox").css("visibility","visible");
	
	OtherFeeEntryBox=$("#OtherFeeEntryBox").dialog({
			title: '其它费用录入',
			width: 700,
			height: 350,
			fit : true,
			closed: true,
			cache: false,
			modal: true,
			buttons:[{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){}
			},{
				text:'关闭',
				iconCls:'icon-no',
				handler:function(){OtherFeeEntryBox.dialog("close");}
			}]
		});
	$("#OtherFeeEntryBox").css("visibility","visible");
	
	StatementsBox=$("#StatementsBox").dialog({
			title: '结算单录入',
			width: 700,
			height: 350,
			fit : true,
			closed: true,
			cache: false,
			modal: true,
			buttons:[{
				text:'保存',
				iconCls:'icon-save',
				handler:function(){}
			},{
				text:'关闭',
				iconCls:'icon-no',
				handler:function(){StatementsBox.dialog("close");}
			}]
		});
	$("#StatementsBox").css("visibility","visible");
}

function Btn_init()
{
	$("#StatementsQueryBtn").linkbutton({
			onClick:function(){
				InvoiceTable.datagrid({
					url:webpath+"Statements/getInvoices",
					queryParams:{
						client_name:$("#client_name").textbox("getValue"),
						pro_name:$("#pro_name").textbox("getValue"),
						engineer_name:$("#engineer_name").textbox("getValue"),
						pro_time_start:$("#pro_time_start").datebox("getValue"),
						pro_time_end:$("#pro_time_end").datebox("getValue")
						}
					});
			}
			});
}

function Table_init()
{
	InvoiceTable=$("#InvoiceTable").datagrid({
			title:"查询请款单信息",
			method:"post",
			singleSelect:true,
			rownumbers:true,
			fit:true,
			fitColumns:true,
			pageSize : 10,
			pageList : [ 10,15, 25, 35, 45, 55,65,100],
			nowrap : false,
			pagination:true,
			showFooter:true,
			//iconCls:'icon-man',
			columns:[[
				{field:'probillmain_uuid',hidden:true},
				//{field:'probill_number',hidden:true},
				{field:'proarc_number',hidden:true},
				{field:'proarc_uuid',hidden:true},
				{field:'probill_number',title:'请款单编号',fixed:true},
				{field:'client_name',title:'客户名称',fixed:true,width:150},
				{field:'pro_name',title:'项目名称',fixed:true,width:150},
				{field:'engineer_name',title:'工程名称',fixed:true,width:150},
				{field:'pro_address',title:'工程地址',fixed:true,width:100},
				{field:'pro_time',title:'请款时间',fixed:true},
				{field:'settlement_total',title:'未结算金额',fixed:true}
        	]],toolbar: [
			{
			iconCls: 'icon-add',
			text:"添加人工费录入",
			handler: function(){
				var node=InvoiceTable.datagrid("getSelected");
				if(node==null)
				{
					$.messager.alert("错误","请先选请款单,再执行本操作!","error");
					return;
				}
				LaborInputBox.dialog("open");
				LaborInputTable.edatagrid({
					url:webpath+"LaborInput/QueryLaborInput",
						queryParams:{
							probillmain_uuid: node.probillmain_uuid
						}
					});
				}
			},"-",
			{
			iconCls: 'icon-add',
			text:"其他费用录入",
			handler: function(){
				var node=InvoiceTable.datagrid("getSelected");
				if(node==null)
				{
					$.messager.alert("错误","请先选请款单,再执行本操作!","error");
					return;
				}
				OtherFeeEntryBox.dialog("open");
				OtherFeeEntryTable.edatagrid({
					url:webpath+"OtherFeeEntry/QueryOtherFeeEntry",
						queryParams:{
							probillmain_uuid: node.probillmain_uuid
						}
					});
				}
			},"-",
			{
			iconCls: 'icon-add',
			text:"添加结算单",
			handler: function(){
				var node=InvoiceTable.datagrid("getSelected");
				if(node==null)
				{
					$.messager.alert("错误","请先选请款单,再执行本操作!","error");
					return;
				}
				StatementsBox.dialog("open");
				StatementsTable.edatagrid({
					url:webpath+"Statements/getStatements",
						queryParams:{
							probillmain_uuid: node.probillmain_uuid
						}
					});
				
				}
			}
			]
		});
}
//其它费用录入
function OtherFeeEntryTable_init(){
var setting={
			method:"post",
			//url:webpath+"LaborInput/QueryLaborInput",
			saveUrl: webpath+"OtherFeeEntry/SaveOtherFeeEntry",
            //updateUrl: webpath+'DeviceManage/EditClassify',
            //destroyUrl: webpath+'DeviceManage/DelClassify',
			idField:"othercost_uuid",
			singleSelect:true,
			rownumbers:true,
			fit:true,
			fitColumns:true,
			//pageSize : 10,
			//pageList : [ 10,15, 25, 35, 45, 55,65,100],
			nowrap : false,
			//pagination:true,
			showFooter:true,
			//iconCls:'icon-man',
			columns:[[
				{field:'othercost_uuid',hidden:true},
				{field:'probillmain_uuid',hidden:true},
				{field:'proarc_number',hidden:true},
				{field:'proarc_uuid',hidden:true},
				{field:'gen_time',title:'费用产生时间',fixed:true,width:100,
				editor:{type:'datebox',options:{required:true,validType:"checkDate"}}},
				{field:'proitems_uuid',title:'品目名称',fixed:true,width:100,formatter:proitems_display,
				editor:{type:'combobox',options:{required:true,valueField:'proitems_uuid',textField:'item_name',
				data:proitems_data}}},
				{field:'amount_money',title:'金额',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'remark',title:'备注',fixed:true,width:400,
				editor:{type:'textbox'}}
        	]],
			onBeforeEdit:function(index,row){
				OtherFeeEntryTableindex=index;
			},
			onEndEdit:function(index,row,changes){
				OtherFeeEntryTableindex=-1;
			},
			onLoadSuccess:function(data){
				OtherFeeEntryTableindex=-1;
			},
			loadFilter:function(data){
				return aspnet_date(data,"gen_time");
			},
			onAdd:function(index, row)
			{
				var node=InvoiceTable.datagrid("getSelected");
				row.probillmain_uuid=node.probillmain_uuid;
				row.proarc_number=node.proarc_number;
				row.proarc_uuid=node.proarc_uuid;
			}
			,
			toolbar: [
				{
				iconCls: 'icon-add',
				text:"添加",
				handler: function(){
					$(OtherFeeEntryTable).edatagrid('addRow');
					}
				},
				{
				iconCls: 'icon-remove',
				text:"删除",
				handler: function(){
					$(OtherFeeEntryTable).edatagrid('destroyRow');
					}
				},
				{
				iconCls: 'icon-save',
				text:"保存",
				handler:OtherFeeEntrySaveBnt
				},{
				iconCls: 'icon-reload',
				text:"刷新",
				handler:function(){
					$(OtherFeeEntryTable).edatagrid("reload");
				}
				}
			]
		};
	OtherFeeEntryTable=$("#OtherFeeEntryTable").edatagrid(setting);

}

function OtherFeeEntrySaveBnt()
{
	$(OtherFeeEntryTable).edatagrid('saveRow');
}

//显示月份数据
function month_display(value,row,index)
{
	for(var i=0;i<month_date.length;i++)
	{
		if(month_date[i].id==parseInt(value))
		{
			return month_date[i].text;
		}
	}
	return "";
}

//显示品目数据
function proitems_display(value,row,index)
{
	for(var i=0;i<proitems_data.length;i++)
	{
		if(proitems_data[i].proitems_uuid==value)
		{
			return proitems_data[i].item_name;
		}
	}
	return "";
}
//人工费录入
function LaborInputTable_init(){
	
	var setting={
			
			method:"post",
			//url:webpath+"LaborInput/QueryLaborInput",
			saveUrl: webpath+"LaborInput/SaveLaborInput",
            //updateUrl: webpath+'DeviceManage/EditClassify',
            //destroyUrl: webpath+'DeviceManage/DelClassify',
			idField:"laborcost_uuid",
			singleSelect:true,
			rownumbers:true,
			fit:true,
			fitColumns:true,
			//pageSize : 10,
			//pageList : [ 10,15, 25, 35, 45, 55,65,100],
			nowrap : false,
			//pagination:true,
			showFooter:true,
			//iconCls:'icon-man',
			columns:[[
				{field:'laborcost_uuid',hidden:true},
				{field:'probillmain_uuid',hidden:true},
				{field:'proarc_number',hidden:true},
				{field:'proarc_uuid',hidden:true},
				{field:'release_month',title:'月份',fixed:true,width:60,formatter:month_display,
				editor:{type:'combobox',options:{required:true,valueField:'id',textField:'text',data:month_date}}},
				{field:'per_name',title:'人员姓名',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true}}},
				{field:'id_number',title:'人员身份证号',fixed:true,width:150,
				editor:{type:'validatebox',options:{required:true,validType:"checkNum"}}},
				{field:'real_rent',title:'实签租金',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'wage_month',title:'月工资',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'ground_time',title:'在勤日期',fixed:true,width:100,
				editor:{type:'datebox',options:{required:true,validType:"checkDate",onChange:LaborInput_date}}},
				{field:'departure_time',title:'离岗日期',fixed:true,width:100,
				editor:{type:'datebox',options:{required:true,validType:"checkDate",onChange:LaborInput_date}}},
				{field:'work_day',title:'在职时间',fixed:true,width:100,
				editor:{type:'textbox',options:{editable:false,validType:"checkNum"}}},
				{field:'commission',title:'提成',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}}
        	]],
			onBeforeEdit:function(index,row){
				LaborInputTablerowindex=index;
			},
			onEndEdit:function(index,row,changes){
				LaborInputTablerowindex=-1;
			},
			onLoadSuccess:function(data){
				LaborInputTablerowindex=-1;
			},
			loadFilter:function(data){
				return aspnet_date(data,"ground_time|departure_time");
			},
			onAdd:function(index, row)
			{
				var node=InvoiceTable.datagrid("getSelected");
				row.probillmain_uuid=node.probillmain_uuid;
				row.proarc_number=node.proarc_number;
				row.proarc_uuid=node.proarc_uuid;
			}
			,
			toolbar: [
				{
				iconCls: 'icon-add',
				text:"添加",
				handler: function(){
					$(LaborInputTable).edatagrid('addRow');
					}
				},
				{
				iconCls: 'icon-remove',
				text:"删除",
				handler: function(){
					$(LaborInputTable).edatagrid('destroyRow');
					}
				},
				{
				iconCls: 'icon-save',
				text:"保存",
				handler:LaborInputSaveBnt
				},{
				iconCls: 'icon-reload',
				text:"刷新",
				handler:function(){
					$(LaborInputTable).edatagrid("reload");
				}
				}
			]
		};
	LaborInputTable=$("#LaborInputTable").edatagrid(setting);
}

function LaborInput_date(newValue,oldValue){
	var ground_time=LaborInputTable.edatagrid('getEditor', {index:LaborInputTablerowindex,field:"ground_time"});
	var departure_time=LaborInputTable.edatagrid('getEditor', {index:LaborInputTablerowindex,field:"departure_time"});
	var work_day=LaborInputTable.edatagrid('getEditor', {index:LaborInputTablerowindex,field:"work_day"});
	var reg=/^[1|2]\d{3}-\d{1,2}-\d{1,2}$/;
	if(ground_time!=null&&departure_time!=null)
	{
		var ground=$(ground_time.target).datebox("getValue");
		var departure=$(departure_time.target).datebox("getValue");
		if(reg.test(ground)&&reg.test(departure))
		{
			var date1=new Date(ground);
			var date2=new Date(departure);
			if(date1.getMonth()!=date2.getMonth())
			{
				$.messager.alert('提示','请务跨月选取在勤时间！','info');
				$(work_day.target).textbox("setValue","");
				return;
			}
			var dat1 = date1.getTime();
			var dat2 = date2.getTime();
			var str = Math.floor((dat2 - dat1)/1000/60/60/24);
			if(str<0)
			{
					$.messager.alert('提示','离岗日期不能小于在勤日期！','info');
					$(work_day.target).textbox("setValue","");
					return;
			}
			$(work_day.target).textbox("setValue",str+1);
		}
	}
	
}

function LaborInputSaveBnt()
{
	$(LaborInputTable).edatagrid('saveRow');
	/*$.messager.confirm('确认','确认保存人工费信息?',function(r){
		if(r){
				
			}
	});*/					
}

//结算单
function StatementsTable_Init()
{	
	var setting={
			
			method:"post",
			//url:webpath+"LaborInput/QueryLaborInput",
			//saveUrl: webpath+"LaborInput/SaveLaborInput",
            //updateUrl: webpath+'DeviceManage/EditClassify',
            //destroyUrl: webpath+'DeviceManage/DelClassify',
			idField:"laborcost_uuid",
			singleSelect:true,
			rownumbers:true,
			fit:true,
			fitColumns:true,
			//pageSize : 10,
			//pageList : [ 10,15, 25, 35, 45, 55,65,100],
			nowrap : false,
			//pagination:true,
			showFooter:true,
			//iconCls:'icon-man',
			columns:[[
				{field:'proarc_uuid',hidden:true},
				{field:'probill_number',hidden:true},
				{field:'proarc_number',hidden:true},
				{field:'setsheet_uuid',hidden:true},
				{field:'probillmain_uuid',hidden:true},
				{field:'settlement_month',title:'月份',fixed:true,width:60,
				editor:{type:'textbox',options:{editable:false}}},
				{field:'month_rent',title:'当月租金',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'othersercharg',title:'其它服务费',fixed:true,width:150,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'rec_rent',title:'应收租金',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'real_rent',title:'实签租金',fixed:true,width:100,
				editor:{type:'textbox',options:{required:true,validType:"checkFloat",onChange:Rent_receivable}}},
				{field:'paid_rent',title:'实收租金',fixed:true,width:100,
				editor:{type:'textbox',options:{required:true,validType:"checkFloat",onChange:Rent_receivable}}},
				{field:'owe_rent',title:'尚欠租金',fixed:true,width:100,
				editor:{type:'textbox',options:{validType:"checkFloat",editable:false}}},
				{field:'collection_time',title:'收款时间',fixed:true,width:100,
				editor:{type:'datebox',options:{required:true,validType:"checkDate",onChange:Statements_date}}},
				{field:'collection_amount',title:'收款金额',fixed:true,width:100,
				editor:{type:'textbox',options:{required:true,validType:"checkFloat"}}},
				{field:'pay_method',title:'收款方式',fixed:true,width:100,
				editor:{type:'validatebox',options:{required:true,validType:"checkFloat"}}},
				{field:'sign_time',title:'签单时间',fixed:true,width:100,
				editor:{type:'datebox',options:{required:true,validType:"checkDate",onChange:Statements_date}}},
				{field:'pay_period',title:'回款周期',fixed:true,width:100,
				editor:{type:'textbox',options:{editable:false}}}
        	]],
			onBeforeEdit:function(index,row){
				StatementsTablerowindex=index;
			},
			onEndEdit:function(index,row,changes){
				StatementsTablerowindex=-1;
			},
			onLoadSuccess:function(data){
				StatementsTablerowindex=-1;
			},
			loadFilter:function(data){
				return aspnet_date(data,"ground_time|departure_time");
			},
			onAdd:function(index, row)
			{
				var node=InvoiceTable.datagrid("getSelected");
				//row.probillmain_uuid=node.probillmain_uuid;
				//row.proarc_number=node.proarc_number;
				//row.proarc_uuid=node.proarc_uuid;
			}
			,
			toolbar: [
				/*{
				iconCls: 'icon-add',
				text:"添加",
				handler: function(){
					$(StatementsTable).edatagrid('addRow');
					}
				},*/
				{
				iconCls: 'icon-remove',
				text:"删除",
				handler: function(){
					$(StatementsTable).edatagrid('destroyRow');
					}
				},
				{
				iconCls: 'icon-save',
				text:"保存",
				handler:LaborInputSaveBnt
				},{
				iconCls: 'icon-reload',
				text:"刷新",
				handler:function(){
					$(StatementsTable).edatagrid("reload");
				}
				}
			]
		};
	StatementsTable=$("#StatementsTable").edatagrid(setting);
}

function StatementsSaveBnt()
{
	$(StatementsTable).edatagrid('saveRow');
}

//租金计算
function Rent_receivable(newValue,oldValue)
{
	//实签租金
	var real_rent=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"real_rent"});
	//实收租金
	var paid_rent=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"paid_rent"});
	//尚欠租金
	var owe_rent=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"owe_rent"});
	//收款金额
	var collection_amount=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"collection_amount"});
	
	
	if(real_rent!=null&&paid_rent!=null&&owe_rent!=null)
	{
		var paid=$(paid_rent.target).textbox("getValue");
		$(collection_amount.target).textbox("setValue",paid);
	}
	
}

function Statements_date(newValue,oldValue)
{
	var collection_time=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"collection_time"});
	var sign_time=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"sign_time"});
	var pay_period=StatementsTable.edatagrid('getEditor', {index:StatementsTablerowindex,field:"pay_period"});
	
	if(collection_time!=null&&sign_time!=null)
	{
		var collection=$(collection_time.target).datebox("getValue");
		var sign=$(sign_time.target).datebox("getValue");
		var reg=/^[1|2]\d{3}-\d{1,2}-\d{1,2}$/;	
		if(reg.test(collection)&&reg.test(sign))
		{	
			var date1=new Date(collection);
			var date2=new Date(sign);
			var dat1 = date1.getTime();
			var dat2 = date2.getTime();
			var str = Math.floor((dat2 - dat1)/1000/60/60/24);
			if(str<0)
			{
					$.messager.alert('提示','离岗日期不能小于在勤日期！','info');
					$(work_day.target).textbox("setValue","");
					return;
			}
			$(pay_period.target).textbox("setValue",str+1);
		}
	}
	
}




